package cn.whit.blog.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.whit.blog.domain.Statistical;
import cn.whit.blog.domain.StatisticalType;
import cn.whit.blog.domain.User;
import cn.whit.blog.domain.Vote;
import cn.whit.blog.domain.VoteItem;
import cn.whit.blog.domain.VoteOption;
import cn.whit.blog.service.VoteService;
import cn.whit.blog.util.BeanFactory;
import cn.whit.blog.util.DynamicQuery;
import cn.whit.blog.util.MyBeanUtils;
import cn.whit.blog.util.PageModel;
import cn.whit.blog.util.Parameter;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

public class VoteServlet extends BaseServlet{
	 
	private static final long serialVersionUID = -552663747513621946L;
	
	VoteService voteService = (VoteService)BeanFactory.createObject("voteService");
	
	//批量删除数据
	public String delete(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
		System.out.println(request.getParameter("ids"));
		String ids = request.getParameter("ids");
		
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		
		if(ids != null) {
			String[] split = ids.split(",");
			System.out.println(split.length);
			for(String s: split) {
				System.out.println(s);
				voteService.deleteById(s);
			}
			writer.write("删除成功".toString());
			return null;
		}
		writer.write("删除失败，请联系管理员".toString());
		return null;
	}
	
	//根据id返回 json 
	public String getById(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String id = request.getParameter("id");
		
		//转json字符串数组返回客户端
		Vote one = voteService.getById(id);
		
		JSONObject jsonStr = JSONObject.fromObject(one);
		response.setCharacterEncoding("utf-8");
		PrintWriter writer = response.getWriter();
		
		writer.write(jsonStr.toString());
		return null;
	}
	
	public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {
		Vote vote = MyBeanUtils.populate2(Vote.class, request.getParameterMap());
		
		String[] options = request.getParameterValues("options");
		for(String s : options) {
			System.out.println(s);
		}
		//投票选项值是数组，封装成 [{id:1,name:'aa',num:0},{id:2,name:'bb',num:0}] 来存储
		List<VoteOption> voteItems = new ArrayList<VoteOption>();
		if(options != null && options.length > 0) {
			for(int i=1; i <= options.length; i++) {
				VoteOption voteOption = new VoteOption(i+"", options[i-1], 0);
				voteItems.add(voteOption);
			}
		}
		// 转为json字符串存储
		JSONArray jsonArray = JSONArray.fromObject(voteItems);
		System.out.println(jsonArray.toString());
		vote.setOptions(jsonArray.toString());
		
		vote.setCreateTime(new Date());
		vote.setStatus(0);
		voteService.save(vote);
		 
		response.sendRedirect(request.getContextPath()+"/VoteServlet?method=list");
		return null;
	}
	
	//去投票列表首页
	public String voteList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//所有当前正在进行(查询条件，时间范围）的投票数据，todo，这里先拿出所有
		
		String num = request.getParameter("num");
		String nameSelected = request.getParameter("nameSelected");
		System.out.println(nameSelected);
		if(num == null) {
			num = "1";
		}
		//获取当前页
		int curNum = Integer.parseInt(num);
		int pageSize  = 5 ;
		DynamicQuery query = new DynamicQuery(); 
		if(nameSelected != null && !("".equals(nameSelected))) {
			//查询参数如果要验证数据类型等，在这里执行
			query.addParameter(new Parameter("title", "like", "%"+nameSelected+"%"));
			request.setAttribute("title", nameSelected);
		}
		PageModel pm = voteService.findByPageAndQuery2(curNum, pageSize, query);
		request.setAttribute("page", pm);
		
		return "/view/vote/list.jsp";
	}
	
	public String list(HttpServletRequest request, HttpServletResponse response) throws Exception {
		//num是传入的当前页
		String num = request.getParameter("num");
		String nameSelected = request.getParameter("nameSelected");
		System.out.println(nameSelected);
		if(num == null) {
			num = "1";
		}
		//获取当前页
		int curNum = Integer.parseInt(num);
		int pageSize  = 5 ;
		DynamicQuery query = new DynamicQuery(); 
		if(nameSelected != null && !("".equals(nameSelected))) {
			//查询参数如果要验证数据类型等，在这里执行
			query.addParameter(new Parameter("title", "like", "%"+nameSelected+"%"));
			request.setAttribute("title", nameSelected);
		}
		PageModel pm = voteService.findByPageAndQuery(curNum, pageSize, query);
		request.setAttribute("page", pm);
		return "/jsp/vote/list.jsp";
	}
	
	 

}
